---@class Battle.RedPlayer:Battle.PlayerRobot
local RedPlayer = Battle.CreateClass("RedPlayer",Battle.PlayerRobot)
Battle.RedPlayer = RedPlayer

function RedPlayer:OnInitialize()
    Battle.PlayerRobot.OnInitialize(self)
    self.checkedGroup = {}
    self.updateInterval = 1
    self.updateTime = 0
    ---@type Battle.Card[]
    self.canUseList = {}
    self.weightList = {}
end
function RedPlayer:Update(dt)
    Battle.PlayerRobot.Update(self,dt)
    if Battle.ClientBattle.battleStage < Battle.BattleStage.Run then
        return
    end
    self.updateTime = self.updateTime + dt
    if self.updateTime < self.updateInterval then return end
    self.updateTime = 0
    self:UpdatePutCard(dt)
    self:UpdatePutSkill(dt)
end
function RedPlayer:UpdatePutCard(dt)
    if not self:CheckSceneEnergy() then return end
    local card = self:GetRandomHandCard()
    if not card then return end
    for k,v in pairs(self.handCards) do
        if card == v then
            self:UseHandCard(k)
            local area = Battle.SceneMgr.areaList["right"]
            local x,y = Battle.BattleUtil.GetRandomPosInRect(area.minX,area.minZ,area.maxX,area.maxZ)

            self:CreateCardEntity(card.heroId,x,y,self.camp == Battle.CampType.Red and 1 or -1)
            break
        end
    end
end
function RedPlayer:UpdatePutSkill(dt)
    local list = Battle.ActorManager.GetActorList()
    for k,v in pairs(list) do
        if v.camp == self.camp then
            local skill = v.skillSystem:GetSkill1()
            if skill and skill.skillData.skillType == Battle.SkillType.Active then
                self:CheckPutSkill(v,skill)
            end
        end
    end
end
---@param skill Battle.Skill
---@param actor Battle.Entity
function RedPlayer:CheckPutSkill(actor,skill)
    if not skill:CheckUseSkill() then return end
    local list = skill.emitter:CalcDamageTargetList()
    if #list == 0 then return end
    skill.emitter:SetHandPos(list[1].pos)
    actor.aiSystem:SetBlackboardValue("hand_skill",true)
end
---@return Battle.Card
function RedPlayer:GetRandomHandCard()
    table.clear(self.canUseList)
    for k,v in pairs(self.handCards) do
        if self.energy >= v.cost then
            table.insert(self.canUseList,v)
        end
    end
    if #self.canUseList == 0 then return end
    if #self.canUseList == 1 then
        return self.canUseList[1]
    end
    table.clear(self.weightList)
    local total = 0
    for k,v in pairs(self.canUseList) do
        local weight = math.floor(10000/(v.cost - 0.5*self.energy)^2+1)
        table.insert(self.weightList,weight)
        total = total + weight
    end
    local r = math.random(0,total)
    for k,v in ipairs(self.weightList) do
        if r <= v then
            return self.canUseList[k]
        end
        r = r - v
    end
    return nil
end
function RedPlayer:CheckSceneEnergy()
    local list = Battle.ActorManager.GetActorList()
    local enemyCount = 0
    local count = 0
    table.clear(self.checkedGroup)
    for k,v in pairs(list) do
        local flag = v.groupId == 0 or (v.groupId > 0 and not self.checkedGroup[v.groupId])
        if v.groupId > 0 and not self.checkedGroup[v.groupId] then
            self.checkedGroup[v.groupId] = true
            flag = true
        elseif v.groupId == 0 then
            flag = true
        end
        if flag then
            local e = DataTable.GetById("t_hero",v.modelId,"f_ConsumeEnergy")
            if v.camp == self.camp then
                count = count + e
            else
                enemyCount = enemyCount + e
            end
        end
    end
    return count < enemyCount
end

